<?php
    require_once 'global.php';
    require_once 'myPDO.php';
    require_once 'mp3.php';

    class Playlists
    {

        private $_id;
        private $_nom;
        private $_id_usuari;


        public static function findbyidusuari($idUsuari) {
            $dsn = DSN_BBDD;
            $user = USER_BBDD;
            $password = PASS_BBDD;

            try {
                $dbh = new mypdo($dsn, $user, $password);
            } catch (pdoexception $e) {
                echo 'connection failed: ' . $e->getmessage();
            }

            $sql = 'SELECT id FROM playlists WHERE id_usuari= :variable';

            $sth = $dbh->prepare($sql);
            $bindparam = $sth->bindparam(':variable', $idUsuari);

            $sth->execute();
            $result = $sth->fetchAll(PDO::FETCH_ASSOC);


            $sth->closecursor();
            $dbh = null;

            $playlistsresult = null;
            foreach ($result as $array) {
                foreach ($array as $id) {
                    $playlistsresult[] = new Playlists($id);
                }
            }
            return $playlistsresult;
        }


        // constructora
        public function __construct($playlistsid = false) {
            if(!$playlistsid) return;

            $dsn = DSN_BBDD;
            $user = USER_BBDD;
            $password = PASS_BBDD;

            try {
                $dbh = new mypdo($dsn, $user, $password);
            } catch (pdoexception $e) {
                echo 'connection failed: ' . $e->getmessage();
            }

            $sql = 'SELECT * FROM playlists WHERE id= :id';

            $sth = $dbh->prepare($sql);
            $bindparam = $sth->bindparam(':id', $playlistsid);

            $sth->execute();
            $result = $sth->fetch(PDO::FETCH_ASSOC);

            $sth->closecursor();
            $dbh = null;

            if (!$result) throw new exception("identifiador de playlists: '{$playlistsid}' no existe.");

            foreach ($result as $attr => $value) $this->{'_'.$attr} = $value;
        }


        public function insert() {

            $dsn = DSN_BBDD;
            $user = USER_BBDD;
            $password = PASS_BBDD;

            // Compruebo si la playlists esta en la BBDD

            try {
                $dbh = new myPDO($dsn, $user, $password);
            } catch (PDOException $e) {
                echo 'Connection failed: ' . $e->getMessage();
            }

            $sql = 'SELECT id FROM playlists WHERE nom= :1 AND id_usuari= :2';

            $sth = $dbh->prepare($sql);
            $bindParam = $sth->bindParam(':1', $this->_nom);
            $bindParam = $sth->bindParam(':2', $this->_id_usuari);

            $sth->execute();
            $result = $sth->fetch(PDO::FETCH_ASSOC);

            $sth->closeCursor();
            $dbh = null;

            if (!$result) {
                // Si la playlists no esta la introduzco en la BBDD
                try {
                    $dbh = new myPDO($dsn, $user, $password);
                } catch (PDOException $e) {
                    echo 'Connection failed: ' . $e->getMessage();
                }

                $sql = 'INSERT INTO playlists (nom, id_usuari) VALUES (:1, :2)';

                $sth = $dbh->prepare($sql);
                $bindParam = $sth->bindParam(':1', $this->_nom);
                $bindParam = $sth->bindParam(':2', $this->_id_usuari);

                $sth->execute();
                $sth->closeCursor();
                $dbh = null;                
            }
         }

        public function update() {
            if (!$this->_id) throw new Exception("Playlists needs id playlists to call update.");

            $dsn = DSN_BBDD;
            $user = USER_BBDD;
            $password = PASS_BBDD;

            try {
                $dbh = new myPDO($dsn, $user, $password);
            } catch (PDOException $e) {
                echo 'Connection failed: ' . $e->getMessage();
            }

            $sql = 'UPDATE playlists SET nom= :2, id_usuari= :3  WHERE id= :1';

            $sth = $dbh->prepare($sql);
            $bindParam = $sth->bindParam(':1', $this->_id);
            $bindParam = $sth->bindParam(':2', $this->_nom);
            $bindParam = $sth->bindParam(':3', $this->_id_usuari);

            $sth->execute();
            $sth->closeCursor();
            $dbh = null;
         }

         public function delete() {
            if (!$this->_id) throw new Exception("Playlists object has no id playlists");

            $dsn = DSN_BBDD;
            $user = USER_BBDD;
            $password = PASS_BBDD;

            try {
                $dbh = new myPDO($dsn, $user, $password);
            } catch (PDOException $e) {
                echo 'Connection failed: ' . $e->getMessage();
            }

            $sql = 'DELETE FROM playlists WHERE id= :1';

            $sth = $dbh->prepare($sql);
            $bindParam = $sth->bindParam(':1', $this->_id);

            $sth->execute();
            $sth->closeCursor();
            $dbh = null;
         }


        private function comprovar_nom($nom) {

        }

        private function comprovar_idusuari($nom) {

        }

        /* publicas */

        // gets
        public function get_id() {
            return $this->_id;
        }

        public function get_nom() {
            return $this->_nom;
        }

        public function get_idusuari() {
            return $this->_id_usuari;
        }


        //sets
        public function set_nom($nom) {
            $this->comprovar_nom($nom);
            $this->_nom = $nom;
        }

        public function set_idusuari($idusuari) {
            $this->comprovar_idusuari($idusuari);
            $this->_id_usuari = $idusuari;
        }
        
        public function songs() {
            $dsn = DSN_BBDD;
            $user = USER_BBDD;
            $password = PASS_BBDD;

            try {
                $dbh = new mypdo($dsn, $user, $password);
            } catch (pdoexception $e) {
                echo 'connection failed: ' . $e->getmessage();
            }

            $sql = 'SELECT mp3 FROM playlistsmp3 WHERE playlist= :variable';

            $sth = $dbh->prepare($sql);
            $bindparam = $sth->bindparam(':variable', $this->_id);

            $sth->execute();
            $result = $sth->fetchAll(PDO::FETCH_ASSOC);

            $sth->closecursor();
            $dbh = null;

            $cancoresult = null;
            foreach ($result as $array) {
                foreach ($array as $id) {
                    $cancoresult[] = new Mp3($id);
                }
            }
            return $cancoresult;
            
        }

        public function add_song($idsong) {
            $dsn = DSN_BBDD;
            $user = USER_BBDD;
            $password = PASS_BBDD;

            try {
                $dbh = new myPDO($dsn, $user, $password);
            } catch (PDOException $e) {
                echo 'Connection failed: ' . $e->getMessage();
            }

            $sql = 'INSERT INTO playlistsmp3 (playlist, mp3) VALUES (:1, :2)';

            $sth = $dbh->prepare($sql);
            $bindParam = $sth->bindParam(':1', $this->_id);
            $bindParam = $sth->bindParam(':2', $idsong);

            $sth->execute();
            $sth->closeCursor();
            $dbh = null;

        }

        public function delete_song($idsong) {
            $dsn = DSN_BBDD;
            $user = USER_BBDD;
            $password = PASS_BBDD;

            try {
                $dbh = new myPDO($dsn, $user, $password);
            } catch (PDOException $e) {
                echo 'Connection failed: ' . $e->getMessage();
            }

            $sql = 'DELETE FROM playlistsmp3 WHERE playlist= :1 AND mp3= :2';

            $sth = $dbh->prepare($sql);
            $bindParam = $sth->bindParam(':1', $this->_id);
            $bindParam = $sth->bindParam(':2', $idsong);

            $sth->execute();
            $sth->closeCursor();
            $dbh = null;

            Mp3::refreshprivate($idsong);
        }

    }

?>
